<?php

// @formatter:off
/**
 * @file status.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: status.php 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/extensions/Repositories/repositories/status.php $
 */
// @formatter:on
class ActionControllerRepositoriesStatus extends WCActionController {
	//
	// Public methods.
	public function check() {
		$check = parent::check();

		if($check == WC_HTTPERROR_OK) {
			global $wcProfile;

			if(!$wcProfile->allowedTo(WC_E_REPOSITORIES_ALLOWED)) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_FORBIDDEN;
				$errno = $check;
				$error = "You don't have enough priviliges";
			}
		}

		return $check;
	}
	//
	// Protected methods.
	protected function subRun() {
		$ok = true;

		global $wcGroup;

		$this->_manager->PATH = "Repositories Status";

		$repo = WCEComicsRepository::Instance($wcGroup);
		$repoImg = WCEImagesRepository::Instance($wcGroup);

		$repoDir = $repo->mainDirectory();

		$this->_manager->repo_offset = "";
		$this->_manager->irepo_offset = "";
		if($repoDir) {
			$this->_manager->repo_id = $repoDir->id();
			$this->_manager->repo_path = $repoDir->name();
			$this->_manager->repo_count = $repoDir->count == 0 ? "Ok" : $repoDir->count;
			$this->_manager->repo_icount = $repoDir->icount == 0 ? "Ok" : $repoDir->icount;

			if($repo->broken()) {
				$this->_manager->repo_status = "Broken";
			} elseif($repo->balanced()) {
				$this->_manager->repo_status = "Balanced";
			} else {
				$this->_manager->repo_unbalanced = true;
				$this->_manager->repo_status = "Unbalanced";
				$this->_manager->repo_bad_id = $repo->badDirectory()->id;
				$this->_manager->repo_bad_name = $repo->badDirectory()->name;
				$this->_manager->repo_offset = $repo->badDirectory()->count - $repo->correctionSize();
				if($repo->balancer()) {
					$this->_manager->repo_balancer_id = $repo->balancer()->id;
					$this->_manager->repo_balancer_name = $repo->balancer()->name;
				} else {
					$this->_manager->repo_balancer_id = "";
					$this->_manager->repo_balancer_name = "";
				}
			}

			if($repoImg->broken()) {
				$this->_manager->irepo_status = "Broken";
			} elseif($repoImg->balanced()) {
				$this->_manager->irepo_status = "Balanced";
			} else {
				$this->_manager->irepo_unbalanced = true;
				$this->_manager->irepo_status = "Unbalanced";
				$this->_manager->irepo_bad_id = $repoImg->badDirectory()->id;
				$this->_manager->irepo_bad_name = $repoImg->badDirectory()->name;
				$this->_manager->irepo_offset = $repoImg->badDirectory()->icount - $repoImg->correctionSize();
				if($repoImg->balancer()) {
					$this->_manager->irepo_balancer_id = $repoImg->balancer()->id;
					$this->_manager->irepo_balancer_name = $repoImg->balancer()->name;
				} else {
					$this->_manager->irepo_balancer_id = "";
					$this->_manager->irepo_balancer_name = "";
				}
			}
		} else {
			$this->_manager->repo_id = "unknown";
			$this->_manager->repo_path = WC_E_REPOSITORIES_MAINDIR;
			$this->_manager->repo_count = $repoDir->count;
			$this->_manager->repo_icount = $repoDir->icount;
			$this->_manager->repo_status = "Broken";
		}

		$directories = $repo->directories();
		uasort($directories, "WCDirectory::NameSort");

		$list = array();
		foreach($directories as $dir) {
			$aux = array(
				"id" => $dir->id,
				"name" => $dir->name,
				"count" => $dir->count,
				"icount" => $dir->icount
			);

			$aux["status"] = "";
			if($dir->count > WC_E_REPOSITORIES_MAX) {
				$aux["status"] .= "Overload";
			} elseif($dir->count == WC_E_REPOSITORIES_MAX) {
				$aux["status"] .= "Full";
			} elseif($dir->count == 0) {
				$aux["status"] .= "Free";
			} else {
				$aux["status"] .= "Ok";
			}
			$aux["status"] .= " - ";
			if($dir->icount > WC_E_REPOSITORIES_IMAX) {
				$aux["status"] .= "Overload";
			} elseif($dir->icount == WC_E_REPOSITORIES_IMAX) {
				$aux["status"] .= "Full";
			} elseif($dir->icount == 0) {
				$aux["status"] .= "Free";
			} else {
				$aux["status"] .= "Ok";
			}

			$list[] = $aux;
		}

		$this->_manager->repositories = $list;

		return $ok;
	}
}
